package com.example.childcheck;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class HttpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        Button button_web=(Button) this.findViewById(R.id.button_baidu);
        button_web.setOnClickListener(new webopen());
        Button button_get=(Button) this.findViewById(R.id.button_get);
        button_get.setOnClickListener(new httpget());
    }

    private class webopen implements View.OnClickListener{
        @Override
        public void onClick(View v){
            Intent intent=new Intent(HttpActivity.this,WebActivity.class);
            startActivity(intent);
        }
    }

    private class httpget implements View.OnClickListener{
        @Override
        public void onClick(View v){
            Socket socket = null;
            try {
                socket = new Socket("www.childcheck.top", 8888);
                DataInputStream in = new DataInputStream(socket.getInputStream());
                String data = in.readUTF();
                String[] values = data.split(",");
                TextView n1=findViewById(R.id.textView_t1);
                TextView n2=findViewById(R.id.textView_t2);
                n1.setText(values[0]);
                n2.setText(values[1]);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}